﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Shpm.App.Commons.Utils;
using Shpm.App.Input.Bean;
using Shpm.App.Input.Dao;
using Shpm.App.Commons.Db.Beans.Master;
using Shpm.System;
using Shpm.App.Search.Dao;
using Shpm.App.Search.Bean;

public partial class App_Input_MemberHaveForm : System.Web.UI.Page
{
    protected string employeeId = string.Empty;
    protected string employeeName = string.Empty;
    protected string personId = string.Empty;
    protected string userId = string.Empty;
    private const int JOB_ID_DOCTOR = 34;
    protected void Page_Load(object sender, EventArgs e)
    {
        SysUserManager manager = new SysUserManager(Context);
        SysUserBean sysUserBean = manager.getSysUserBean();

        if (sysUserBean != null)
        {
            MstEmployeeBean empBean = sysUserBean.getEmployeeBean();
            MstSysUserBean mstSysUserBean =
                sysUserBean == null ? null : sysUserBean.getMstSysUserBean();
            if (mstSysUserBean != null)
            {
                this.userId = mstSysUserBean == null ? "" : mstSysUserBean.SysUserId;
            }
            if (empBean != null)
            {
                this.employeeId = empBean.EmployeeId;
                this.employeeName = empBean.EmployeeName;
                this.personId = Convert.ToString(empBean.PersonId);
                //this.employeeJob = empBean.EmployeeJob;
                //this.employeeLev = empBean.EmployeeLevel;


                //this.employeeJob = "1".Equals(empBean.EmployeeLevel) ? "主管医生" : "保健医生";

                //IList dataList = null;
                //查找JOB_ID

                PersonEmployeeDao personEmployeeDao = new PersonEmployeeDao();
                PersonEmployeeBean personEmployeeBean = new PersonEmployeeBean();
                personEmployeeBean.EmployeeId = empBean.EmployeeId;
                personEmployeeBean = personEmployeeDao.Find(personEmployeeBean);
                //保健医生
                if (JOB_ID_DOCTOR.Equals(personEmployeeBean.JobId))
                {
                    ViewState["JOB"] = "doc";
                }
                else
                {
                    ViewState["JOB"] = "cml";
                }
            }
        }

        if (!IsPostBack)
        {
            //if (Context.Items["EMPLOYEE_ID"] != null)
            //{
            //    ViewState["EMPLOYEE_ID"] = Context.Items["EMPLOYEE_ID"];
            //}
            //else
            //{
            //    ViewState["EMPLOYEE_ID"] = null;
            //}
            ViewState["EMPLOYEE_ID"] = employeeId;
            ViewState["MemberNo"] = txtMemberNo.Text.Trim();
            ViewState["Name"] = txtMemberName.Text.Trim();
            ViewState["ApartmentNo"] = dropRoom.SelectedValue;
            ViewState["BuildNo"] = dropBuildNo.SelectedValue;
            ViewState["Floor"] = dropFloor.SelectedValue;

            SalesInfoDao dao = new SalesInfoDao();
            SalesInfoBean salesInfoBean = new SalesInfoBean();
            //楼号邦定
            IList list = dao.FindBuilds();
            if (list != null && list.Count > 0)
            {
                list.Insert(0, salesInfoBean);
            }
            else
            {
                list.Add(salesInfoBean);
            }
            this.dropBuildNo.DataSource = list;
            this.dropBuildNo.DataBind();


            //楼层邦定
            list = new ArrayList();
            list.Add(salesInfoBean);
            this.dropFloor.DataSource = list;
            this.dropFloor.DataBind();

            //房间号邦定
            this.dropRoom.DataSource = list;
            this.dropRoom.DataBind();
        }
    }
    protected void btnSearch_Click(object sender, EventArgs e)
    {
        if (ComValidator1.DoValidator())
        {
            string floor = Request.Form["ctl00$ContentPlaceHolder1$dropFloor"].ToString();
            string room = Request.Form["ctl00$ContentPlaceHolder1$dropRoom"].ToString();
            ViewState["TXT_FLOOR"] = floor;
            ViewState["TXT_ROOM_NO"] = room;
            SalesInfoDao dao = new SalesInfoDao();
            SalesInfoBean salesInfoBean = new SalesInfoBean();
            salesInfoBean.BuildingNo = this.dropBuildNo.SelectedValue;
            //floor
            IList list = dao.FindBuildFloor(salesInfoBean);
            if (list != null && list.Count > 0)
            {
                list.Insert(0, salesInfoBean);
            }
            else
            {
                list = new ArrayList();
                list.Add(salesInfoBean);
            }

            this.dropFloor.DataSource = list;
            this.dropFloor.DataBind();
            this.dropFloor.SelectedValue = floor;

            //room
            salesInfoBean.Floor = floor;
            list = dao.FindBuildRoom(salesInfoBean);
            if (list != null && list.Count > 0)
            {
                list.Insert(0, salesInfoBean);
            }
            else
            {
                list = new ArrayList();
                list.Add(salesInfoBean);
            }

            this.dropRoom.DataSource = list;
            this.dropRoom.DataBind();
            this.dropRoom.SelectedValue = room;

            ViewState["MemberNo"] = txtMemberNo.Text.Trim();
            ViewState["Name"] = txtMemberName.Text.Trim();
            ViewState["ApartmentNo"] = dropRoom.SelectedValue;
            ViewState["BuildNo"] = dropBuildNo.SelectedValue;
            ViewState["Floor"] = dropFloor.SelectedValue;
        }
    }

    protected override void OnPreRender(EventArgs e)
    {
        base.OnPreRender(e);
        if (Convert.ToString(ViewState["JOB"]) == "doc")
        {
            boundDoc();
        }
        else if (Convert.ToString(ViewState["JOB"]) == "cml")
        {
            boundCml();
        }

    }

    private void boundCml()
    {
        HappyChamberlaineBean dataBean = new HappyChamberlaineBean();
        HappyChamberlaineSrhDao Dao = new HappyChamberlaineSrhDao();
        dataBean.EmployeeId = Convert.ToString(ViewState["EMPLOYEE_ID"]);
        dataBean.MemberNo = Convert.ToString(ViewState["MemberNo"]);
        dataBean.Name = Convert.ToString(ViewState["Name"]);
        dataBean.ApartmentNo = Convert.ToString(ViewState["ApartmentNo"]);
        dataBean.BuildNo = Convert.ToString(ViewState["BuildNo"]);
        dataBean.Floor = Convert.ToString(ViewState["Floor"]);
        IList dataList = Dao.ListMenber(dataBean);
        GridView1.DataSource = dataList;
        this.GridView1.DataBind();
    }
    private void boundDoc()
    {
        HealthAssignBean dataBean = new HealthAssignBean();
        HealthAssignDao Dao = new HealthAssignDao();
        dataBean.DoctorId = Convert.ToString(ViewState["EMPLOYEE_ID"]);
        dataBean.MemberNo = Convert.ToString(ViewState["MemberNo"]);
        dataBean.Name = Convert.ToString(ViewState["Name"]);
        dataBean.ApartmentNo = Convert.ToString(ViewState["ApartmentNo"]);
        dataBean.BuildNo = Convert.ToString(ViewState["BuildNo"]);
        dataBean.Floor = Convert.ToString(ViewState["Floor"]);
        IList dataList = Dao.ListMenber(dataBean);
        GridView1.DataSource = dataList;
        this.GridView1.DataBind();
    }
    protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        GridView1.PageIndex = e.NewPageIndex;
    }
    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            if (Convert.ToString(ViewState["JOB"]) == "doc")
            {
                HealthAssignBean bean = (HealthAssignBean)e.Row.DataItem;
                (e.Row.FindControl("hypeMemberNo") as HyperLink).Attributes.Add("onclick", "openPersonDetails('" + bean.MemberNo + "')");
                (e.Row.FindControl("health") as HyperLink).Attributes.Add("onclick", "popupMemberSearch('" + bean.MemberNo + "')");
            }
            else if (Convert.ToString(ViewState["JOB"]) == "cml")
            {
                HappyChamberlaineBean bean = (HappyChamberlaineBean)e.Row.DataItem;
                (e.Row.FindControl("hypeMemberNo") as HyperLink).Attributes.Add("onclick", "openPersonDetails('" + bean.MemberNo + "')");
                (e.Row.FindControl("health") as HyperLink).Attributes.Add("onclick", "popupMemberSearch('" + bean.MemberNo + "')");
            }

           
        }
    }
    protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
    {
        GridViewRow row = GridView1.SelectedRow;
        DataKey key = GridView1.DataKeys[row.DataItemIndex];
        Context.Items["MEMBER_NO"] = key.Value;
        Context.Items["URL"] = "~/App/Input/MemberHaveForm.aspx";
        // 生活模型
        ControllerUtils.Transfer(this.Context, "~/App/Input/LivingMemberLivingForm.aspx", true);

    }
}
